﻿var lsInfo = {
  current: 1,
  pageSize: allPageSize,
  data: [],
  totalRow: 0,
  totalPage: 0,
  oneDate: {},
  editState: false,
  state: 0
};

loading.show();

var brand = new Vue({
  el: '#listTable',
  data: {
    form: resetForm(),
    brandList: []
  },
  methods: {
    add: function () {
      brand.form.Value = brand.form.Text;
      var data = brand.form;

      var validator = new Validator();
      validator.add(data.Text, [{
        strategy: 'isNonEmpty',
        errorMsg: '名称不能为空'
      }]);

      validator.add(data.Logogram, [{
        strategy: 'isNonEmpty',
        errorMsg: '缩写不能为空'
      }]);

      validator.add(data.Explain, [{
        strategy: 'isNonEmpty',
        errorMsg: '说明不能为空'
      }]);

      var errmsg = validator.start();
      if (errmsg) {
        alert(errmsg);
        return false;
      }


      var submitData = {
        Key: data.Key,
        Text: data.Text,
        Value: data.Value,
        Logogram: data.Logogram,
        Explain: data.Explain
      };
      loading.show();
      $.ajax({
        url: '/api/CommonApi?API=DictionaryTableOp',
        type: 'put',
        dataType: 'json',
        data: {
          ApiParamObj: JSON.stringify(submitData)
        }
      })
        .done(function () {
          console.log("success");
          brand.brandList.push(submitData);
          brand.form = resetForm();

          load();
          // loading.hide();
        })
        .fail(function () {
          console.log("error");
          loading.hide();
        })
    },
    edit: function (index) {
      // Vue.set(brand.brandList[index], 'edit', !brand.brandList[index].edit);
      toggleEdit(index);
    },
    update: function (index) {
      var data = brand.brandList[index];
      var submitData = {
        ID: data.ID,
        Text: data.Text,
        Value: data.Text,
        Logogram: data.Logogram,
        Explain: data.Explain
      };
      loading.show();
      $.ajax({
        url: '/api/CommonApi?API=DictionaryTableOp',
        type: 'post',
        dataType: 'json',
        data: {
         ApiParamObj: JSON.stringify(submitData)
        },
      })
        .done(function () {
          console.log("success");
          toggleEdit(index);
          loading.hide();
        })
        .fail(function () {
          console.log("error");
          toggleEdit(index);

          load();
          // loading.hide();
        })
    },
    remove: function (index) {
      var r = confirm('确定删除？');
      if (!r) {
        return false;
      }
      var id = brand.brandList[index].ID;
      var submitData = {
        ID: id,
        IsDelete: 1
      };
      loading.show();
      $.ajax({
        url: '/api/CommonApi?API=DictionaryTableOp',
        type: 'post',
        dataType: 'json',
        data: {
          ApiParamObj: JSON.stringify(submitData)
        },
      })
        .done(function () {
          console.log("success");
          brand.brandList.splice(index, 1);

          load();
          // loading.hide()
        })
        .fail(function () {
          console.log("error");
          loading.hide();
        })
    }
  }
});

function resetForm() {
  return {
    Key: 'PROJECT_BRAND',
    Text: '',
    Value: '',
    Logogram: '',
    Explain: ''
  };
}

function toggleEdit(index) {
  Vue.set(brand.brandList[index], 'edit', !brand.brandList[index].edit);
}

var getList = function (obj) {
  $.ajax({
    cache: false,
    async: false,
    url: '/api/CommonApi?API=GetDictionarys',
    type: 'get',
    data: {
      Key: 'PROJECT_BRAND',
      Text: '',
      Logogram: ''
    },
    dataType: 'json',
    success: function (data) {
      // console.log(data)
      if (data.Success) {
        obj.callback(data)
      }
    }
  });
};

load();
function load() {
  getList({
    size: lsInfo.pageSize,
    index: lsInfo.current,
    callback: function (data) {
      // console.log(JSON.stringify(data));
      brand.brandList = data.ApiParamObj;
      loading.hide();
    }
  });
}

